using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using FiltronASP.Helpers.FileUpload;

namespace FiltronASP.Helpers.HttpObjects
{
    public class HttpObjectsHelper
    {
        /// <summary>
        /// Metoda do pobierania obiektu <code>HttpContext</code>.
        /// </summary>
        /// <returns>obiekt HttpContext</returns>
        public static HttpContext GetHttpContext()
        {
            try
            {
                HttpContext httpContext = HttpContext.Current;
                if (httpContext == null)
                {
                    HttpRequest httpRequest = new HttpRequest("/", UploadConsts.APP_URL, String.Empty);
                    HttpResponse httpResponse = new HttpResponse(new StringWriter());
                    httpContext = new HttpContext(httpRequest, httpResponse);
                }   //if null
                return httpContext;
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Metoda do pobierania obiektu <code>UrlHelper</code>.
        /// </summary>
        /// <returns>obiekt UrlHelper</returns>
        public static UrlHelper GetUrlHelper()
        {
            try
            {
                HttpContext httpContext = HttpObjectsHelper.GetHttpContext();
                HttpContextWrapper httpContextWrapper = new HttpContextWrapper(httpContext);
                return new UrlHelper(new RequestContext(httpContextWrapper, new RouteData()));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Metoda zwracajaca obiekt <code>HttpServerUtility</code>.
        /// </summary>
        /// <returns>obiekt >HttpServerUtility</returns>
        public static HttpServerUtility GetHttpServer()
        {
            try
            {
                HttpContext httpContext = HttpObjectsHelper.GetHttpContext();
                return httpContext.Server;
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return null;
        }

    }
}
